package auto.app.controller;


import auto.app.controller.from.Scheduled.RedisKey;
import auto.app.controller.from.Scheduled.ScheduledAdd;
import auto.app.controller.from.Scheduled.ScheduledPage;
import auto.app.controller.from.Scheduled.ScheduledUpdate;
import auto.app.controller.info.ScheduledInfo.ScheduledPageInfo;
import auto.app.exetion.HibernateExetion;
import auto.app.exetion.MainExection;
import auto.app.model.Scheduled;
import auto.app.service.ScheduledService;
import auto.app.util.CommonUtil;
import auto.app.util.Result;
import cn.hutool.core.util.ObjectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javafx.scene.chart.ValueAxis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import static auto.app.exetion.MainExetionMessage.CRONADDTASKKEYNOTNULLERROR;

/**
 * <p>
 * 定时任务配置表 前端控制器
 * </p>
 *
 * @author 杨森
 * @since 2022-09-20
 */
@RestController
@RequestMapping("/scheduled")
@Api(value = "定时任务",tags = "定时任务")
public class ScheduledController {

    @Autowired
    private ScheduledService scheduledService;

    /**
     * 该接口已在2023-01-28 接入xxl-job后放弃
     */
    @PostMapping(value = "/redisKey",name ="设置定时任务执行合集")
    @ApiOperation(value = "设置定时任务执行合集",notes = "设置定时任务执行合集")
    public Result<Boolean> redisKey(@Valid @RequestBody RedisKey redisKey) throws MainExection {
        Boolean aBoolean = scheduledService.redisKey(redisKey.getTaskKey(), redisKey.getCollectId());
        return Result.getInstance(Boolean.class).setData(aBoolean);
    }

    @PostMapping(name = "新增定时任务")
    @ApiOperation(value = "新增定时任务",notes = "新增定时任务")
    public Result<Boolean> addScheduled(@Valid @RequestBody ScheduledAdd scheduledAdd) throws MainExection {
        Boolean aBoolean = scheduledService.addScheduled(scheduledAdd);
        return Result.getInstance(Boolean.class).setData(aBoolean);
    }

    @PostMapping(value = "/list",name = "定时任务列表")
    @ApiOperation(value = "定时任务列表",notes = "定时任务列表")
    public Result<ScheduledPageInfo> list(@RequestBody ScheduledPage scheduledPage) throws MainExection {
        ScheduledPageInfo scheduledPageInfo = scheduledService.list(scheduledPage);
        return Result.getInstance(ScheduledPageInfo.class).setData(scheduledPageInfo);
    }

    @GetMapping(value = "/start/{id}",name = "启动/停止定时任务")
    @ApiOperation(value = "启动/停止定时任务",notes = "启动/停止定时任务")
    public Result<Boolean> start(@PathVariable Integer id) throws MainExection {
        Boolean start = scheduledService.start(id);
        return Result.getInstance(Boolean.class).setData(start);
    }

    @DeleteMapping(value = "/delete/{id}",name = "删除定时任务")
    @ApiOperation(value = "删除定时任务",notes = "删除定时任务")
    public Result<Boolean> delete(@PathVariable Integer id) throws MainExection {
        Boolean start = scheduledService.delete(id);
        return Result.getInstance(Boolean.class).setData(start);
    }



    @PutMapping(value = "/update",name = "修改定时任务")
    @ApiOperation(value = "修改定时任务",notes = "修改定时任务")
    public Result<Boolean> update(@Valid @RequestBody ScheduledUpdate scheduledUpdate) throws MainExection {
        Boolean aBoolean = scheduledService.updateScheduled(scheduledUpdate);
        return Result.getInstance(Boolean.class).setData(aBoolean);
    }

    @GetMapping(value = "/{id}",name = "获取定时任务详情")
    @ApiOperation(value = "获取定时任务详情",notes = "获取定时任务详情")
    public Result<Scheduled> getDetail(@PathVariable Integer id) throws MainExection {
        Scheduled serviceById = scheduledService.getById(id);
        if (ObjectUtil.isNull(serviceById)){
            throw new MainExection(CRONADDTASKKEYNOTNULLERROR.getCode(),CRONADDTASKKEYNOTNULLERROR.getMsg());
        }
        return Result.getInstance(Scheduled.class).setData(serviceById);
    }
}

